import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jc.constant.PolicyAttachmentTypeConst;
import com.jc.util.HttpPostUtil;
import com.jc.util.R;
import com.jc.vo.ApiConfig;
import com.jc.vo.PolicyAttachment;
import com.jc.vo.PolicyVO;
import com.jc.vo.QuotaApplyVO;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CrossOrderDemo {

    // jc提供的SecretId
    public static String SECRET_ID = "100002";
    // jc提供的SecretKey
    public static String SECRET_KEY = "2a4afaf10a0ebd926dc91387373a0594";


    public static String SERVICE_HOST = "127.0.0.1:8080";

    public static String INSURE_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/createPolicy";


    @Test
    public void testCreatePolicy() throws ParseException {
        PolicyVO vo =getvo("2");

        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, INSURE_SERVICE_URL);
        try {

            ObjectMapper om = new ObjectMapper();
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            String response = HttpPostUtil.send(om.writeValueAsString(vo), config, false);
            R r = om.readValue(response, R.class);
            try {
                if (r.getSuccess()) {
                    QuotaApplyVO result = om.convertValue(r.getContent(), QuotaApplyVO.class);
                    r.setContent(result);
                }
            } catch (Exception e) {
                r.setSuccess(false);
                r.setCode(R.RCODE.SYS_ERROR.getCode());
                r.setMessage(R.RCODE.SYS_ERROR.getMessage());
                e.printStackTrace();
            }
            System.out.println(r.getMessage());
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private PolicyVO getvo(String productCode){
        PolicyVO vo =new PolicyVO();

        vo.setProductCode(productCode);
        vo.setStartTime(new Date());
        vo.setDispatchType(2); //2  快递派	1; 卡车派	2
        vo.setTransportType(2);
        vo.setShelfGuarantee(3);
        vo.setInsuranceConfirmType(1); // 保额确定方式 1 加成比例  2.货值加运费
        vo.setGoodsValueAmount(new BigDecimal("1"));
        vo.setCurrency("CNY");
        vo.setFromCountry("CHN");
        vo.setFromAddress("宁波");


//        vo.setTransferCountry("JPA");

        if (2== vo.getInsuranceConfirmType()){
            vo.setDeliveryAmount(new BigDecimal("2000"));
            vo.setDeliveryCurrency("RMB");
        }

//        vo.setExpressTrackingNumberFile("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg;https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");
//        vo.setGoodsNoteFile("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg;https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");
//        vo.setGoodsPackageImages("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg;https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");


        vo.setToCountry("US");
        vo.setToAddress("洛杉矶");

        vo.setInsured("深圳天玺货运跨境有限公司");
        vo.setInsuredType(new Byte("2"));
        vo.setDestType(new Byte("1"));
//        vo.setOrderNumber("TEST"+new Date().getTime());
        vo.setOrderNumber("TEST1758091337757");
        vo.setWeightPackaged(new BigDecimal("125.5"));
        vo.setTransportName("CA1234");
        vo.setWaybillNumber("BL0SD032232");
        vo.setGoodsPackage("1");
        vo.setPackageCount(33);
        vo.setGoodsType(1);
        vo.setGoodsNote("100台智能手机");
        //加成比例
        vo.setAppendPercent(new BigDecimal("100"));

        vo.setWarehousingNumber("ST0323232323");


        vo.setLostCareOnly(false); //保丢不保损
        vo.setWarProtection(false); //战争险保障
        vo.setIncorrectDispatchProtection(true); //送错地址保障
        vo.setReturnProtection(false); //回邮附加责任
        vo.setRiotProtection(false);//暴动
        vo.setCreator("张三");






        List<PolicyAttachment> attachmentList =new ArrayList<>();

        PolicyAttachment attachment =new PolicyAttachment();
        attachment.setType(PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_CARGO_DESCRIPTION);
        attachment.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");

        PolicyAttachment attachment2 =new PolicyAttachment();
        attachment2.setType(PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_CARGO_DESCRIPTION);
        attachment2.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");





        PolicyAttachment attachment3 =new PolicyAttachment();
        attachment3.setType(PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_TRACKING_NUMBER);
        attachment3.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");

        PolicyAttachment attachment4 =new PolicyAttachment();
        attachment4.setType(PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_TRACKING_NUMBER);
        attachment4.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");



        PolicyAttachment attachment5 =new PolicyAttachment();
        attachment5.setType(PolicyAttachmentTypeConst.FRAGILE_GOODS_INTERNAL_PACKAGING_PHOTO);
        attachment5.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");

        PolicyAttachment attachment6 =new PolicyAttachment();
        attachment6.setType(PolicyAttachmentTypeConst.FRAGILE_GOODS_INTERNAL_PACKAGING_PHOTO);
        attachment6.setUrl("https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg");






        attachmentList.add(attachment);
        attachmentList.add(attachment2);
        attachmentList.add(attachment3);
        attachmentList.add(attachment4);
        attachmentList.add(attachment5);
        attachmentList.add(attachment6);

//        vo.setAttachmentList(attachmentList);


        return vo;
    }







    public static String SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/calculatePolicy";


    @Test
    public void testCalculate() throws ParseException {
        PolicyVO vo =getVo1("2");
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, SERVICE_URL);
        try {

            ObjectMapper om = new ObjectMapper();
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            String response = HttpPostUtil.send(om.writeValueAsString(vo), config, false);
            R r = om.readValue(response, R.class);
            try {
                if (r.getSuccess()) {
                    QuotaApplyVO result = om.convertValue(r.getContent(), QuotaApplyVO.class);
                    r.setContent(result);
                }
            } catch (Exception e) {
                r.setSuccess(false);
                r.setCode(R.RCODE.SYS_ERROR.getCode());
                r.setMessage(R.RCODE.SYS_ERROR.getMessage());
                e.printStackTrace();
            }
            System.out.println(r.getMessage());
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private PolicyVO getVo1(String productCode){
        PolicyVO vo =new PolicyVO();
        vo.setProductCode(productCode);
        //保额确定方式  货值x加成比例	1 ; 货值加运费	2
        vo.setInsuranceConfirmType(2);
//        vo.setInsuranceConfirmType(1);
        vo.setGoodsValueAmount(new BigDecimal("50000"));
        vo.setCurrency("USD");

        if (2 == vo.getInsuranceConfirmType()) {
            vo.setDeliveryCurrency("RMB");
            vo.setDeliveryAmount(new BigDecimal("2000"));
        }
        if (1 == vo.getInsuranceConfirmType()) {
            vo.setAppendPercent(new BigDecimal("110"));
        }


        if("2".equalsIgnoreCase(productCode)) {
            vo.setTransportType(2);
            vo.setDispatchType(2);
            vo.setShelfGuarantee(3);
            vo.setWeightPackaged(new BigDecimal("20"));
        }else{
            vo.setWarehouseTermsRequired(true);
        }

        return vo;

    }





}
